# !/usr/bin python3
# encoding: utf-8 -*-
# @file: count_method.py
# @author   : wjd
# @Time: 2023/12/1 11:04
import decimal
import math
from _decimal import Decimal

from antalpha_course.Antalpha_test.tools_api.xxljob_api import GenerateBillInterestHandlerTest, BillInterestHandler


class Count():

    def chufa_8(self, a, b):
        """
        除法
        :param b:
        :return:
        """
        print("分子:%s" % a)
        print("分母:%s" % b)
        sum = decimal.Decimal(a) / decimal.Decimal(b)  # 计算折合U
        print(f"截断18位前：【{sum}】")
        a = str(sum).split('.')[0] + '.' + str(sum).split('.')[1][:8]
        s = Decimal(a)
        print(f"截断 8位后：【{s}】")
        print("\n")

    def chufa_32(self, a, b):
        """
        除法
        :param b:
        :return:
        """
        print("分子:%s" % a,type(a))
        print("分母:%s" % b,type(b))
        sum = decimal.Decimal(a) / decimal.Decimal(b)
        result_str = format(sum, ".40f")
        print(f"截断40位前：【{result_str}】")
        a = str(result_str).split('.')[0] + '.' + str(result_str).split('.')[1][:32]
        s = Decimal(a)
        result_str = format(s, ".32f")
        # print(result_str)
        print(f"截断32位后：【{result_str}】")
        # print(type(result_str))
        # print("\n")
        return result_str

    def jiafa(self, l: list):
        """
        加法
        :return:
        """
        sum1 = 0
        for i in l:
            sum1 += i
        return sum1

    def multiply(self, l: list):
        """
        乘法
        :return:
        """
        for i in l:
            print(i)
            sum = decimal.Decimal(i[0]) * decimal.Decimal(i[1])  # 折合USD
            if i[2] == "USD":
                print(f"{i[2]}截断18位前：【{sum}】")
                a = str(sum).split('.')[0] + '.' + str(sum).split('.')[1][:2]
                s = Decimal(a)
                print(f"{i[2]}截断 2位后：【{s}】")
                print("\n")
            else:
                print(f"{i[2]}截断18位前：【{sum}】")
                a = str(sum).split('.')[0] + '.' + str(sum).split('.')[1][:8]
                s = Decimal(a)
                print(f"{i[2]}截断 8位后：【{s}】")
                print("\n")

    def truncate(self, num, n):
        """
        截断浮点数
        :param n:
        :return:
        """
        # 计算整数位数
        int_part = int(math.floor(math.log10(abs(num))))
        # print(int_part) # -1
        # 计算截断位数
        n = int_part - n + 1
        # print(n) # -18
        print(num)
        print("截取：")
        print(round(num, -n))
        print("************")

        # 截断浮点数
        return round(num, -n)

    def once_interest(self, l: list):  # [[][]]
        """
        一次性利息
        :return:
        """
        # 年化利率 *（合同本金 - 已还本金）* 天数 / 365
        sum1 = 0
        for i in l:
            # print(i)
            a = decimal.Decimal(i[0])  # 年华利率
            # print(a)
            b = decimal.Decimal(i[1])  # 合同本金
            # print(b)
            c = decimal.Decimal(i[2])  # 已还本金
            # print(c)
            d = decimal.Decimal(i[3])  # 天数
            # print(d)

            sum = (a / 100 * (b - c) * d) / 365
            print(f"******{(b - c)}")
            # truncatesum = truncate(sum, 18)
            print(f"截断18位前：【{sum}】")
            a = str(sum).split('.')[0] + '.' + str(sum).split('.')[1][:18]
            s = Decimal(a)
            print(f"截断18位后：【{s}】")

            print(f"当前sum1值是：{sum1}")
            sum1 += s
            print(f"加上【{s}】后:是【{sum1}】")
            print('\n')
            # print(sum1)

        return sum1


if __name__ == '__main__':
    import decimal
    # （okGasPrice * 10^9 * gasMultiple * gasLimit / 10^18）=结果（18位向下取整）
    # 59548687466 * 3.3 * 21000 / 10 ^ 18
    b  = decimal.Decimal(59548687466)*decimal.Decimal(3.3)*decimal.Decimal(21000)
    print("=======")
    print(59548687466*3.3)
    print("===========")
    print(decimal.Decimal(59548687466)*decimal.Decimal(3.3))
    a = (196510668637*21000)
    Count().chufa_32(a,1000000000000000000)


    # 设置全局精度
    # decimal.getcontext().prec = 50

    # 使用字符串来表示浮点数，保留原始的精度
    # a = "0.99999999"
    # b = "31536000"
    #
    # print("分子:%s" % a)
    # print("分母:%s" % b)

    # 使用 Decimal 对象进行计算
    # sum_decimal = decimal.Decimal(a) / decimal.Decimal(b)

    # 将 Decimal 对象格式化为科学计数法字符串
    # formatted_result = format(sum_decimal, ".30f")
    # print(f"截断32位前：【{formatted_result}】")

    pass
    # print(chufa("20", "1.2222"))
    # flage = 1
    # if flage == 1:
    #     id_ = 12
    #     if id_ == 11:
    #         generateBillInterestHandlerTest = GenerateBillInterestHandlerTest()
    #         res1 = generateBillInterestHandlerTest.send()
    #         print(res1.json())
    #     else:
    #         # 生成定制化借贷利息 账单日的转一天 utcNow='2023-12-5 18:00:00'
    #         billInterestHandlertest = BillInterestHandler(orderId=18066961913223,
    #                                                       utcNow='2023-9-10 18:00:00')  # '2023-2-1 16:00:00' 15061952379885
    #         res2 = billInterestHandlertest.send()
    #         print("生成定制化借贷利息")
    #         print(res2.json())

    # 生成质押借贷利息
    # SetPrincipalOrMonthlyInterestTest = SetPrincipalOrMonthlyInterestTest(orderId=13066818989214,
    #                                                                       utcNow='2023-12-1 00:00:00')  # '2023-2-1 16:00:00' 15061952379885
    # res2 = SetPrincipalOrMonthlyInterestTest.send()
    # print(res2.json())
    # else:
    # 年化利率 *（合同本金 - 已还本金）* 天数 / 365
    # a = 年化利率
    # b = 合同本金
    # c = 已还本金
    # d = 天数
    # list_ = [[77.7777, 50000, 0, 19]]

    # list_ = [[6, 50, 0, 9], [7, 50, 0, 12], [7, 50, 8.8, 4]]
    # [1.5, 5000, 0, 148]]  # [64.1234, 44321.12345678, 11000, 15], [74.1234, 44321.12345678, 11000, 1]]
    # list_ = [29386.96, 1847.21, 0.99]
    # print(jiafa(l=list_))
    # print("计算结果:{}".format(once_interest(l=list_)))
    # print("页面结果:%s" % '0.22064657')
    # flag = 'ETH'
    # Prime_Sp_Dashboard = Prime_Sp_Dashboard()
    # res = Prime_Sp_Dashboard.send()
    # print(res)
    # walletCoin2CoinPriceResponseMap = res.json()["result"]["walletCoin2CoinPriceResponseMap"]
    # for i in walletCoin2CoinPriceResponseMap:
    #
    #     if i == flag + '-USD':
    #         print(f"{flag}计算折合USD:")
    #         a = multiply(
    #             [["10", f"{walletCoin2CoinPriceResponseMap[i]['price']}", "USD"]])
    #     elif i == flag + '-BTC':
    #         print(f"{flag}计算折合BTC:")
    #         a = multiply(
    #             [["10", f"{walletCoin2CoinPriceResponseMap[i]['price']}", "BTC"]])
    #     pass
    # a = multiply([["5.42846875", "33789.23892389", "USD"], ["0.0000002", "34271.92684389", "BTC"]])
